
* data
use "data/inputs/MunicipioData_AD_Chavez.dta", clear

* reshape
reshape long share_pres, i(estado municipio) j(year, string)
gen party = "AD" if regexm(year, "AD")
replace party = "OFIC" if regexm(year, "OFIC")
destring year, ignore("AD" "OFIC") replace

* rank
egen obs = count(share_pres), by(year)
sort year share_pres
by year: gen rank = _n
gen percentile = rank / obs

* Unión
#delimit;
twoway  (line percentile year if municipio == "Unión" & estado == "Falcón",
		 lcolor(black) lwidth(medthick))
		(line percentile year if municipio == "Unión" & estado == "Falcón" 
		& year >= 1993 & year <= 2000,
		 lcolor(blue) lwidth(medthick))
	    (scatter percentile year if municipio == "Unión" & estado == "Falcón" 
		 & year < 1998, mcolor(black) 
	    mlwidth(thick) mlcolor(white) msize(large)) 
		(scatter percentile year if municipio == "Unión" & estado == "Falcón" 
		 & year >= 1998, mcolor(black) msymbol(diamond)
	    mlwidth(thick) mlcolor(white) msize(large))
	    (scatter percentile year if municipio == "Unión" & estado == "Falcón" 
		 & year == 1998, mcolor(blue) msymbol(diamond)
	    mlwidth(thick) mlcolor(white) msize(large))	,		
		graphregion(fcolor(white) lcolor(white) margin(zero))
        plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none))
        xsize(3) ysize(3)
		title("", size(large))
		ytitle("Vote Share Percentile", color(black) size(large))
		xtitle("")
		ylabel(0(.2)1, labsize(large) glcolor(black%10) glwidth(thin) angle(horizontal))
		xlabel(1958 "1958"
		1968 "1968"
		1978 "1978"
		1988 "1988"
		1998 "98   "
		2000 "   00"
		2012 "2012", labsize(large))
		xscale( range(1958 2014))
        legend(off);
graph export "figures/Figure2a.pdf", replace;

* Maracaibo
#delimit;

twoway  (line percentile year if municipio == "Maracaibo" & estado == "Zulia",
		 lcolor(black) lwidth(medthick))
		(line percentile year if municipio == "Maracaibo" & estado == "Zulia" 
		& year >= 1993 & year <= 2000,
		 lcolor(blue) lwidth(medthick)) 
	    (scatter percentile year if municipio == "Maracaibo" & estado == "Zulia" 
		 & year < 1998, mcolor(black) 
	    mlwidth(thick) mlcolor(white) msize(large)) 
		(scatter percentile year if municipio == "Maracaibo" & estado == "Zulia" 
		 & year >= 1998, mcolor(black) msymbol(diamond)
	    mlwidth(thick) mlcolor(white) msize(large))
	    (scatter percentile year if municipio == "Maracaibo" & estado == "Zulia" 
		 & year == 1998, mcolor(blue) msymbol(diamond)
	    mlwidth(thick) mlcolor(white) msize(large))	,		
		graphregion(fcolor(white) lcolor(white) margin(zero))
        plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none))
        xsize(3) ysize(3)
		title("", size(large))
		ytitle("", color(black) size(large))
		xtitle("")
		yscale(off range(0 1))
		ylabel(0(.2)1, glcolor(black%10) glwidth(thin))
		xlabel(1958 "1958"
		1968 "1968"
		1978 "1978"
		1988 "1988"
		1998 "98   "
		2000 "   00"
		2012 "2012", labsize(large))
		xscale( range(1956 2014))
        legend(off);
graph export "figures/Figure2b.pdf", replace;

* Rojas and Chacao
#delimit;
twoway  (line percentile year if municipio == "Chacao" & estado == "Miranda",
		 lcolor(black) lwidth(medthick))
		 
		(line percentile year if municipio == "Chacao" & estado == "Miranda" 
		& year >= 1993 & year <= 2000,
		 lcolor(blue) lwidth(medthick)) 
		 
	    (scatter percentile year if municipio == "Chacao" & estado == "Miranda"
		& year < 1998, mcolor(black) 
	    mlwidth(thick) mlcolor(white) msize(large)) 
		
		(scatter percentile year if municipio == "Chacao" & estado == "Miranda" 
		 & year >= 1998, mcolor(black) msymbol(diamond)
	    mlwidth(thick) mlcolor(white) msize(large))
		
	    (scatter percentile year if municipio == "Chacao" & estado == "Miranda" 
		 & year == 1998, mcolor(blue) msymbol(diamond)
	    mlwidth(thick) mlcolor(white) msize(large))
		
		(line percentile year if municipio == "Rojas" & estado == "Barinas",
		 lcolor(black) lwidth(medthick))
		 
		(line percentile year if municipio == "Rojas" & estado == "Barinas" 
		& year >= 1993 & year <= 2000,
		 lcolor(blue) lwidth(medthick)) 
		 
	    (scatter percentile year if municipio == "Rojas" & estado == "Barinas" 
		 & year < 1998, mcolor(black) 
	    mlwidth(thick) mlcolor(white) msize(large)) 
		
		(scatter percentile year if municipio == "Rojas" & estado == "Barinas" 
		 & year >= 1998, mcolor(black) msymbol(diamond)
	    mlwidth(thick) mlcolor(white) msize(large))
		
		(scatter percentile year if municipio == "Rojas" & estado == "Barinas" 
		 & year == 1998, mcolor(blue) msymbol(diamond)
	    mlwidth(thick) mlcolor(white) msize(large)),
		
		graphregion(fcolor(white) lcolor(white) margin(zero))
        plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none))
        xsize(3) ysize(3)
		title("", size(large))
		ytitle("", color(black) size(large))
		xtitle("")
        yscale(off range(0 1))
		ylabel(0(.2)1, glcolor(black%10) glwidth(thin))
		xlabel(1958 "1958"
		1968 "1968"
		1978 "1978"
		1988 "1988"
		1998 "98   "
		2000 "   00"
		2012 "2012", labsize(large))
		xscale( range(1956 2014))
        legend(off);
graph export "figures/Figure2c.pdf", replace;


* End


